<?php

require('configs/include.php');
include_once 'classes/detalle.php';
require(C_FULL_PATH . 'modules/m_phpass/PasswordHash.php');

class c_index extends super_controller {

    //save in session user data
    public function set_user_session($user) {
        $_SESSION['empleado']['nombre'] = $user->get('nombre');
        $_SESSION['empleado']['identificacion'] = $user->get('identificacion');
        $_SESSION['empleado']['cargo'] = $user->get('cargo');
        $_SESSION['empleado']['empleado'] = $user->get('identificacion');
        $this->session = $_SESSION;
    }

    //destroy session
    public function destroy_session() {
        session_destroy();
        unset($this->session);
    }

    public function login() {
        if (is_empty($this->post->identificacion)) {
            throw_exception("El empleado no puede ser vacio");
        } else if (is_empty($this->post->contrasena)) {
            throw_exception("La contraseña no puede ser vacia");
        }
        $cod['empleado']['contrasena'] = $this->post->contrasena;
        $cod['empleado']['identificacion'] = $this->post->identificacion;
        $options['empleado']['lvl2'] = "one_login";

        $this->orm->connect();
        $this->orm->read_data(array("empleado"), $options, $cod);
        $empleado = $this->orm->get_objects("empleado");
        $this->orm->close();

        if (empty($empleado[0])) {
            throw_exception($this->gvar['m_incorrect_login']);
        } else {
            $this->set_user_session($empleado[0]);
            $this->msg_warning = "Logueado correctamente";
            $this->type_warning = "success";
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }
    }

    public function logout() {
        $this->destroy_session();
        $this->type_warning = "success";
        $this->msg_warning = "Logout Complete";
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
        $this->temp_aux = 'message.tpl';
    }

    public function display() {
        if ($this->error == 1) {
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }
        
        switch ($this->session['empleado']['cargo']){
            case "ADMINISTRADOR":
                $this->engine->assign('title', $this->gvar['n_global']);
                $this->engine->assign('active', $this->gvar['n_index']);
                $this->engine->display('header.tpl');
                
                $this->engine->display($this->temp_aux);

                
                $this->engine->assign('empleado',$this->session['empleado']['nombre']);
                $this->engine->display('register.tpl');
                $this->engine->display('footer.tpl');                
                break;
            case "OPERARIO FABRICACION":
                header('Location: http://localhost/glight/crear_reporte.php');
                break;            
                
                
            case "GERENTE":
                header('Location: http://localhost/glight/orden_de_produccion.php');
                break;
                 
            case "PINTOR":
                
                header('Location: http://localhost/glight/crear_reporte.php');

                break;
            case "ENSAMBLADOR MOTOR":
                header('Location: http://localhost/glight/crear_reporte.php');

                break;
            case "MECANICO":
                header('Location: http://localhost/glight/crear_reporte.php');

                break;
            case "ENSAMBLADOR MOTO":
                
                header('Location: http://localhost/glight/crear_reporte.php');

                break;
            case "CONTROLADOR RODAMIENTO":
                header('Location: http://localhost/glight/crear_reporte.php');
         
                break;
            case "CONTROLADOR CALIDAD":
                header('Location: http://localhost/glight/crear_reporte.php');
  
                break;
            case "DISEÑADOR":
                header('Location: http://localhost/glight/Crear_Nuevo_Diseno.php');

 
                break;

            case "SOLDADOR":
                
                header('Location: http://localhost/glight/crear_reporte.php');

                break;   
                     
            default:
                $this->engine->assign('title', $this->gvar['n_global']);
                $this->engine->assign('active', $this->gvar['n_index']);
                $this->engine->display('header.tpl');
                
                $this->engine->display($this->temp_aux);

                
                $this->engine->display('index.tpl');
                $this->engine->display('footer.tpl');
                break;
        } 
        
        
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
        }
        $this->display();
    }
}

$call = new c_index();
$call->run();
?>